﻿// ===============================================================================
// Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:张谦
// ===============================================================================

using System;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;
using XCI.Annotations;
using XCI.Core;
using XCI.Sys.Validator;

namespace XCI.Sys.Model
{
    /// <summary>
    /// 系统用户
    /// </summary>
    [ApiModel(Caption = "系统用户")]
    [NewUserPassword]
    [NewUserEntId]
    public class SysUser
    {
        /// <summary>
        /// 用户主键
        /// </summary>
        [Display(Name = "用户主键")]
        [ApiModelProperty(PrimaryKey = true)]
        public string Id { get; set; }

        /// <summary>
        /// 账号
        /// </summary>
        [Display(Name = "账号")]
        [Required(ErrorMessage = "请输入账号")]
        [StringLength(100, ErrorMessage = "账号不能超过100个字符")]
        public string Account { get; set; }

        /// <summary>
        /// 用户姓名
        /// </summary>
        [Display(Name = "用户姓名")]
        [Required(ErrorMessage = "请输入用户姓名")]
        [StringLength(100, ErrorMessage = "用户姓名不能超过100个字符")]
        public string Name { get; set; }

        /// <summary>
        /// 用户姓名简拼
        /// </summary>
        [Display(Name = "用户姓名简拼")]
        [StringLength(100, ErrorMessage = "用户姓名简拼不能超过100个字符")]
        public string Spell { get; set; }

        /// <summary>
        /// 账号类型 [1-企业账号, 0-系统账号]
        /// </summary>
        [Required(ErrorMessage = "请输入账号类型")]
        [Display(Name = "账号类型")]
        public int Category { get; set; }

        /// <summary>
        /// 账号类型名称
        /// </summary>
        [Display(Name = "账号类型名称")]
        public string CategoryName { get; set; }

        /// <summary>
        /// 机构主键
        /// </summary>
        [Display(Name = "机构主键")]
        [Required(ErrorMessage = "请选择机构")]
        public string DeptId { get; set; }

        /// <summary>
        /// 机构名称
        /// </summary>
        [Display(Name = "机构名称")]
        public string DeptName { get; set; }

        /// <summary>
        /// 允许修改密码 [true-是, false-否]
        /// </summary>
        [Display(Name = "允许修改密码")]
        public bool PwdAllowModify { get; set; }

        /// <summary>
        /// 不允许修改密码 [true-是, false-否]
        /// </summary>
        [Display(Name = "不允许修改密码")]
        public bool PwdDenyModify { get; set; }

        /// <summary>
        /// 下次登录必须修改密码 [true-是, false-否]
        /// </summary>
        [Display(Name = "下次登录必须修改密码")]
        public bool PwdNextLoginMustModify { get; set; }

        /// <summary>
        /// 密码永不过期 [1-永不过期, 0-允许过期]
        /// </summary>
        [Display(Name = "密码永不过期")]
        public bool PwdNeverExpire { get; set; }

        /// <summary>
        /// 密码过期时间
        /// </summary>
        [Display(Name = "密码过期时间")]
        public DateTime? PwdExpireDateTime { get; set; }

        /// <summary>
        /// 有效期开始时间
        /// </summary>
        [Display(Name = "有效期开始时间")]
        public DateTime? AllowStartDateTime { get; set; }

        /// <summary>
        /// 有效期结束时间
        /// </summary>
        [Display(Name = "有效期结束时间")]
        [CompareDate(nameof(AllowStartDateTime), DateCompareType.GreatThan)]
        public DateTime? AllowEndDateTime { get; set; }

        /// <summary>
        /// 是否超管 [true-是, false-否]
        /// </summary>
        [Display(Name = "管理员")]
        public bool IsAdmin { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        [Display(Name = "手机号码")]
        [StringLength(100, ErrorMessage = "手机号码不能超过100个字符")]
        [MobilePhone]
        public string Mobile { get; set; }

        /// <summary>
        /// 电子邮件
        /// </summary>
        [Display(Name = "电子邮件")]
        [StringLength(100, ErrorMessage = "电子邮件不能超过100个字符")]
        [Email]
        public string Email { get; set; }

        /// <summary>
        /// 职位
        /// </summary>
        [StringLength(100, ErrorMessage = "职位长度不能超过100")]
        [Display(Name = "职位")]
        public string Post { get; set; }

        /// <summary>
        /// 登录次数
        /// </summary>
        [Display(Name = "登录次数")]
        public int? LoginTimes { get; set; }

        /// <summary>
        /// 首次登录时间
        /// </summary>
        [Display(Name = "首次登录时间")]
        public DateTime? FirstVisitDateTime { get; set; }

        /// <summary>
        /// 最后登录时间
        /// </summary>
        [Display(Name = "最后登录时间")]
        public DateTime? LastVisitDateTime { get; set; }

        /// <summary>
        /// 企业主键
        /// </summary>
        [StringLength(100, ErrorMessage = "企业主键长度不能超过100")]
        [Display(Name = "企业主键")]
        public string EntId { get; set; }

        /// <summary>
        /// 企业名称
        /// </summary>
        [StringLength(100, ErrorMessage = "企业名称长度不能超过100")]
        [Display(Name = "企业名称")]
        public string EntName { get; set; }

        /// <summary>
        /// 是否企业账号
        /// </summary>
        [Display(Name = "企业账号")]
        [JsonIgnore]
        public bool IsEnt
        {
            get => Category == 1;
            set => Category = value ? 1 : 0;
        }

        /// <summary>
        /// 是否超级管理员可见[1-超管可见, 0-全部可见]
        /// </summary>
        [Display(Name = "超管可见")]
        public bool IsAdminVisible { get; set; }

        /// <summary>
        /// 状态 [true-启用, false-禁用]
        /// </summary>
        [Display(Name = "状态")]
        public bool Status { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [Display(Name = "备注")]
        [StringLength(500, ErrorMessage = "备注不能超过500个字符")]
        public string Remark { get; set; }

        /// <summary>
        /// 用户登录凭据
        /// </summary>
        [Display(Name = "用户登录凭据", AutoGenerateField = false)]
        public string Token { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Display(Name = "密码", AutoGenerateField = false)]
        [StringLength(100, ErrorMessage = "密码不能超过100个字符")]
        public string Pwd { get; set; }

        /// <summary>
        /// 确认密码
        /// </summary>
        [Display(Name = "确认密码", AutoGenerateField = false)]
        [JsonIgnore]
        public string ConfirmPwd { get; set; }

        /// <summary>
        /// 密码盐
        /// </summary>
        [Display(Name = "密码盐", AutoGenerateField = false)]
        [StringLength(100, ErrorMessage = "密码盐不能超过100个字符")]
        public string PwdSalt { get; set; }

        /// <summary>
        /// 角色主键字符串,多个逗号隔开
        /// </summary>
        [Display(Name = "角色主键字符串", AutoGenerateField = false)]
        public string RoleIds { get; set; }

        /// <summary>
        /// 账号是否在过期
        /// </summary>
        [Display(Name = "过期")]
        public bool Expire { get; set; }

        /// <summary>
        /// 图片路径
        /// </summary>
        public string PhotoUrl { get; set; }

        /// <summary>
        /// 复制对象
        /// </summary>
        public SysUser Clone()
        {
            return (SysUser)MemberwiseClone();
        }
    }
}
